<?php

namespace App\Http\Controllers\Admin;

use App\Model\Cate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //获取分类模型
        $cates = new Cate();
        $cates = $cates->tree();
        //返回分类页面
        return view('admin.cate.list',compact('cates'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        //获取一级类
        $cate = Cate::where('cate_pid','0')->get();
        return view('admin.cate.add',compact('cate'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //接受添加的分类信息
        $input = $request->all();

        $res = Cate::create($input);

        if ($res){
            $data = [
                'status'=>0,
                'message'=>'添加成功!'
            ];
        }else{
            $data = [
                'status'=>1,
                'message'=>'添加失败!'
            ];
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cate = Cate::find($id);

        return view('admin.cate.edit',compact('cate'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = $request->input();
        $cate = Cate::find($id);
        $cate->cate_name = $input['cate_name'];
        $cate->cate_title = $input['cate_title'];
        if($cate->save()){
            $data = [
                'status'=>0,
                'message'=>'修改成功'
            ];
        }else{
            $data = [
                'status'=>1,
                'message'=>'修改失败'
            ];
        }
        return $data;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function changeOrder(Request $request)
    {
        $input = $request->except('_token');

        $res = Cate::where('cate_id',$input['cate_id'])->update(['cate_order'=>$input['cate_order']]);
        if($res){
            $data = [
                'status'=>0,
                'message'=>'排序修改成功'
            ];
        }else{
            $data = [
                'status'=>1,
                'message'=>'排序修改失败'
            ];
        }
        return $data;
    }
}
